<template>
  <div class="txcel">
    <el-table
      ref="tableOrigin"
      border
      v-bind="options"
      :data="data"
      @sort-change="handleSortChange"
      @selection-change="handleRowSelect"
    >
      <el-table-column
        v-if="rowSelection"
        v-bind="rowSelection.options"
        type="selection"
        align="center"
      ></el-table-column>

      <template v-for="col in columns">
        <el-table-column
          v-if="col.component"
          :key="col.prop"
          :label="col.label"
          :prop="col.prop"
          :sortable="col.sortable"
          :formatter="col.formatter"
          :resizable="false"
          :width="col.width"
          :header-align="col.headerAlign || 'center'"
          :align="col.align || 'center'"
          :min-width="col.minWidth"
        >
          <template slot-scope="scope">
            <component
              :is="genColComponent(col, scope)"
              :row="scope.row"
              :column="col"
            ></component>
          </template>
        </el-table-column>

        <slot v-else-if="col.slot" :name="col.slot"></slot>

        <el-table-column
          v-else
          :key="col.prop"
          :label="col.label"
          :prop="col.prop"
          :sortable="col.sortable"
          :formatter="col.formatter"
          :resizable="false"
          :width="col.width"
          :min-width="col.minWidth"
          :header-align="col.headerAlign || 'center'"
          :align="col.align || 'center'"
        ></el-table-column>
      </template>
    </el-table>
    <el-pagination
      v-if="pager"
      background
      class="txcel_pager"
      :layout="pager.layout"
      :current-page="pagination.page"
      :page-size="pagination.page_size"
      :page-sizes="[10, 20, 50, 100]"
      :total="pager.total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
import { Table, Pagination, TableColumn } from 'element-ui'

export default {
  name: 'Txcel',

  /* eslint-disable */
  components: {
    [Table.name]: Table,
    [Pagination.name]: Pagination,
    [TableColumn.name]: TableColumn,
  },

  provide() {
    return {
      emitCell: this.emitCell,
    }
  },

  props: {
    data: {
      type: Array,
      default: () => [],
    },
    columns: {
      type: [Array, Boolean],
      default: false,
    },
    rowSelection: {
      type: [Object, Boolean],
      default: false,
    },
    pager: {
      type: [Object, Boolean],
      default: () => ({
        layout: 'total, sizes, prev, pager, next, jumper',

        page: 1,
        page_size: 10,
        total: 0,
      }),
    },
    options: {
      type: Object,
      default: () => ({}),
    },
  },

  computed: {
    pagination() {
      return {
        page: this.pager.page,
        page_size: this.pager.page_size,
      }
    },
  },

  methods: {
    getTable() {
      return this.$refs.tableOrigin
    },

    handlePageChange(nextPage) {
      this.$emit(
        'change',
        { ...this.pagination, page: nextPage }, // pagination
        null // ordering
      )
    },
    handleSizeChange(size) {
      this.$emit(
        'change',
        { ...this.pagination, page_size: size }, // pagination
        null // ordering
      )
    },

    handleSortChange(sortInfo) {
      this.$emit(
        'change',
        null, // pagination
        sortInfo // ordering
      )
    },

    handleRowSelect(nextSelected) {
      this.rowSelection.onChange(nextSelected)
    },

    emitCell(prop, data = {}) {
      this.$emit('cellChange', prop, data)
    },

    genColComponent(col, scope) {
      const render = col.render
      const component = col.component
      // 兼容老的component写法
      if (
        component &&
        Object.prototype.toString(component) === '[object Object]'
      ) {
        return component
      }

      // render的写法，更简洁
      if (render && typeof render === 'function') {
        return {
          inject: ['emitCell'], // 可以使用emitCell抛出事件
          render: (h) => render(h, scope),
        }
      }
    },
  },
}
</script>

<style>
.txcel_pager {
  margin-top: 24px;
  text-align: right;
}
</style>
